//
//  MYGMineDynamicView.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/4/8.
//

import UIKit

class MYGMineDynamicView: UIView {

    private var contents:[MYGMicrosrationDyModel] = []
    private var index:Int = 0
    var doctor_id:String?
    var reloadSrcView:((_ index:Int)->Void)?
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
        self.backgroundColor = .white
        NotificationCenter.default.addObserver(self, selector: #selector(reloadTableView), name: NSNotification.Name.init("reloadDyTable"), object: nil)
    }
    
    @objc func reloadTableView() {
        getMineDynmaic(index: index)
    }
    
    func relaodView(index: Int) {
        getMineDynmaic(index: index)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configUI(){
        self.backgroundColor = .white
        addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
  
    private lazy var tableView: UITableView = {
        let tableV = UITableView()
        tableV.backgroundColor = .white
        tableV.delegate = self
        tableV.dataSource = self
        tableV.separatorStyle = .none
        tableV.showsVerticalScrollIndicator = false
        tableV.isScrollEnabled = false
        tableV.register(cellWithClass: MYGMinImageCell.self)
        tableV.register(cellWithClass: MYGMinTitleCell.self)
        tableV.register(cellWithClass: MYGMinContentCell.self)
        return tableV
    }()

}

extension MYGMineDynamicView:UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        contents.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let model = contents[indexPath.row]
        if model.medias.isEmpty {
            return 63.5
        }
        return 110// 63.5//106  image video 110
    }
    //0全部 1图片 2视频 3个人报道
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let model = contents[indexPath.row]
        
        if (model.medias.isEmpty) {
            let cell = tableView.dequeueReusableCell(withClass: MYGMinTitleCell.self, for: indexPath)
            cell.model = model
            return cell
        }else if (model.type == 2 || model.type == 1) {
            let cell = tableView.dequeueReusableCell(withClass: MYGMinImageCell.self, for: indexPath)
            cell.model = model
            return cell
        }else  {
            
            let cell = tableView.dequeueReusableCell(withClass: MYGMinContentCell.self, for: indexPath)
            cell.model = model
            return cell
        }
       
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = MYGFindDetailController(id: contents[indexPath.row].id)
        UIViewController.currentViewController?.navigationController?.pushViewController(vc)
    }
    
}

extension MYGMineDynamicView {
    
    func getMineDynmaic(index:Int) {
        MYGLoading.show()
        MMYGNetwork<MYGMicrosrationApi,MYGPageModel<MYGMicrosrationDyModel>>.request(target: .getDoctorMicro(type: index,doctor_id:doctor_id ?? "")) { [self] arr in
            MYGLoading.hide()
            guard let items = arr else  {
                return
            }
            self.index = index
            contents = items.list
            self.reloadSrcView?(contents.count)
            tableView.reloadData()
            
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
    }
    
    
    
    
}
